<?php
/**
 * <https://y.st./>
 * Copyright © 2019 Alex Yst <mailto:copyright@y.st>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <https://www.gnu.org./licenses/>.
**/

$xhtml = array(
	'<{title}>' => 'Lab: Introduction to R',
	'<{subtitle}>' => 'Written in <span title="Data Mining and Machine Learning">CS 4407</span> by <a href="https://y.st./">Alexand(er|ra) Yst</a>, finalised on 2019-02-06',
	'<{copyright year}>' => '2019',
	'takedown' => '2017-11-01',
	'<{body}>' => <<<END
<p>
	We&apos;re asked to start an interactive R session, then copy a number of commands from the book and paste them into the command line.
	The book makes it a bit difficult to find the addresses of the necessary data files, but one can be found at <a href="https://www-bcf.usc.edu/~gareth/ISL/Auto.data"><code>https://www-bcf.usc.edu/~gareth/ISL/Auto.data</code></a> and the other at <a href="https://www-bcf.usc.edu/~gareth/ISL/Auto.csv"><code>https://www-bcf.usc.edu/~gareth/ISL/Auto.csv</code></a>.
</p>
<h2>R script</h2>
<p>
	Our professor recommended submitting an R script or R Markdown file.
	I don&apos;t know anything about R Markdown, and with the huge reading assignment this week, there wasn&apos;t time to learn.
	Besides, if R Markdown is as flawed as regular Markdown, it&apos;s not worth using anyway.
	Below is my R script.
	Each command I entered during my interactive session is included, but I don&apos;t actually recommend running the script if you&apos;re learning R.
	I found that plotting a graph and then plotting another graph overwrites the first graph, leaving you only with the second.
	In other words, by running the commands as a script like this, you miss things you&apos;d otherwise get to see.
</p>
<blockquote>
<pre><code>x &lt;- c(1, 3, 2, 5)
x
x = c(1, 6, 2)
x
y = c(1, 4, 3)
length(x)
length(y)
x + y
ls()
rm(x, y)
ls()
rm(list = ls())
?matrix
x = matrix(data = c(1, 2, 3, 4), nrow = 2, ncol = 2)
x
x = matrix(c(1, 2, 3, 4), 2, 2)
matrix(c(1, 2, 3, 4), 2, 2, byrow = TRUE)
sqrt(x)
x^2
x = rnorm(50)
y = x + rnorm(50 , mean = 50, sd = 0.1)
cor(x, y)
set.seed(1303)
rnorm(50)
set.seed(3)
y = rnorm(100)
mean(y)
var(y)
sqrt(var(y))
sd(y)
x = rnorm(100)
y = rnorm(100)
plot(x, y)
plot(x, y, xlab = &quot;this is the x-axis&quot;, ylab = &quot;this is the y-axis&quot;, main = &quot;Plot of X vs Y&quot;)
pdf(&quot;Figure.pdf&quot;)
plot(x, y, col = &quot;green&quot;)
dev.off()
x = seq(1 ,10)
x
x = 1:10
x
x = seq(-pi, pi, length = 50)
y = x
f = outer(x, y, function(x, y) cos(y) / (1 + x^2))
contour(x, y, f)
contour(x, y, f, nlevels = 45, add = T)
fa = (f - t(f)) / 2
contour(x, y, fa, nlevels = 15)
image(x, y, fa)
persp(x, y, fa)
persp(x, y, fa, theta = 30)
persp(x, y, fa, theta = 30, phi = 20)
persp(x, y, fa, theta = 30, phi = 70)
persp(x, y, fa, theta = 30, phi = 40)
A = matrix(1:16, 4, 4)
A
A[2, 3]
A[c(1, 3), c(2, 4)]
A[1:3, 2:4]
A[1:2,]
A[, 1:2]
A[1,]
A[-c(1 ,3),]
A[-c(1 ,3), -c(1, 3, 4)]
dim(A)
Auto = read.table(&quot;Auto.data&quot;)
fix(Auto)
Auto = read.table(&quot;Auto.data&quot;, header = T, na.strings = &quot;?&quot;)
fix(Auto)
Auto = read.csv(&quot;Auto.csv&quot;, header = T, na.strings = &quot;?&quot;)
fix(Auto)
dim(Auto)
Auto[1:4,]
Auto = na.omit(Auto)
dim(Auto)
names(Auto)
plot(cylinders, mpg)
plot(Auto\$cylinders, Auto\$mpg)
attach(Auto)
plot(cylinders, mpg)
cylinders = as.factor(cylinders)
plot(cylinders, mpg)
plot(cylinders, mpg, col = &quot;red&quot;)
plot(cylinders, mpg, col = &quot;red&quot;, varwidth = T)
plot(cylinders, mpg, col = &quot;red&quot;, varwidth = T, horizontal = T)
plot(cylinders, mpg, col = &quot;red&quot;, varwidth = T, xlab = &quot;cylinders&quot;, ylab = &quot;MPG&quot;)
hist(mpg)
hist(mpg, col = 2)
hist(mpg, col = 2, breaks = 15)
pairs(Auto)
pairs(~ mpg + displacement + horsepower + weight + acceleration, Auto)
plot(horsepower, mpg)
identify(horsepower, mpg, name)
summary(Auto)
summary(mpg)</code></pre>
</blockquote>
<h2>Interactive session</h2>
<p>
	This is a transcript from my interactive session.
	I&apos;d absolutely love to go over all what happened in this session, but with that huge reading assignment we have in this course this week, as well as the assignments in my other course, there just not time.
</p>
<blockquote>
<pre>&lt;file://bailey.hn.y.st/home/yst&gt;
&gt; R

R version 3.3.3 (2017-03-06) -- &quot;Another Canoe&quot;
Copyright (C) 2017 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type &apos;license()&apos; or &apos;licence()&apos; for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type &apos;contributors()&apos; for more information and
&apos;citation()&apos; on how to cite R or R packages in publications.

Type &apos;demo()&apos; for some demos, &apos;help()&apos; for on-line help, or
&apos;help.start()&apos; for an HTML browser interface to help.
Type &apos;q()&apos; to quit R.

&gt; x &lt;- c(1, 3, 2, 5)
&gt; x
[1] 1 3 2 5
&gt; x = c(1, 6, 2)
&gt; x
[1] 1 6 2
&gt; y = c(1, 4, 3)
&gt; length(x)
[1] 3
&gt; length(y)
[1] 3
&gt; x + y
[1]  2 10  5
&gt; ls()
[1] &quot;x&quot; &quot;y&quot;
&gt; rm(x, y)
&gt; ls()
character(0)
&gt; rm(list = ls())
&gt; ?matrix
&gt; x = matrix(data = c(1, 2, 3, 4), nrow = 2, ncol = 2)
&gt; x
     [,1] [,2]
[1,]    1    3
[2,]    2    4
&gt; x = matrix(c(1, 2, 3, 4), 2, 2)
&gt; matrix(c(1, 2, 3, 4), 2, 2, byrow = TRUE)
     [,1] [,2]
[1,]    1    2
[2,]    3    4
&gt; sqrt(x)
         [,1]     [,2]
[1,] 1.000000 1.732051
[2,] 1.414214 2.000000
&gt; x^2
     [,1] [,2]
[1,]    1    9
[2,]    4   16
&gt; x = rnorm(50)
&gt; y = x + rnorm(50 , mean = 50, sd = 0.1)
&gt; cor(x, y)
[1] 0.9968931
&gt; set.seed(1303)
&gt; rnorm(50)
 [1] -1.1439763145  1.3421293656  2.1853904757  0.5363925179  0.0631929665
 [6]  0.5022344825 -0.0004167247  0.5658198405 -0.5725226890 -1.1102250073
[11] -0.0486871234 -0.6956562176  0.8289174803  0.2066528551 -0.2356745091
[16] -0.5563104914 -0.3647543571  0.8623550343 -0.6307715354  0.3136021252
[21] -0.9314953177  0.8238676185  0.5233707021  0.7069214120  0.4202043256
[26] -0.2690521547 -1.5103172999 -0.6902124766 -0.1434719524 -1.0135274099
[31]  1.5732737361  0.0127465055  0.8726470499  0.4220661905 -0.0188157917
[36]  2.6157489689 -0.6931401748 -0.2663217810 -0.7206364412  1.3677342065
[41]  0.2640073322  0.6321868074 -1.3306509858  0.0268888182  1.0406363208
[46]  1.3120237985 -0.0300020767 -0.2500257125  0.0234144857  1.6598706557
&gt; set.seed(3)
&gt; y = rnorm(100)
&gt; mean(y)
[1] 0.01103557
&gt; var(y)
[1] 0.7328675
&gt; sqrt(var(y))
[1] 0.8560768
&gt; sd(y)
[1] 0.8560768
&gt; x = rnorm(100)
&gt; y = rnorm(100)
&gt; plot(x, y)
&gt; plot(x, y, xlab = &quot;this is the x-axis&quot;, ylab = &quot;this is the y-axis&quot;, main = &quot;Plot of X vs Y&quot;)
&gt; pdf(&quot;Figure.pdf&quot;)
&gt; plot(x, y, col = &quot;green&quot;)
&gt; dev.off()
null device 
          1 
&gt; x = seq(1 ,10)
&gt; x
 [1]  1  2  3  4  5  6  7  8  9 10
&gt; x = 1:10
&gt; x
 [1]  1  2  3  4  5  6  7  8  9 10
&gt; x = seq(-pi, pi, length = 50)
&gt; y = x
&gt; f = outer(x, y, function(x, y) cos(y) / (1 + x^2))
&gt; contour(x, y, f)
&gt; contour(x, y, f, nlevels = 45, add = T)
&gt; fa = (f - t(f)) / 2
&gt; contour(x, y, fa, nlevels = 15)
&gt; image(x, y, fa)
&gt; persp(x, y, fa)
&gt; persp(x, y, fa, theta = 30)
&gt; persp(x, y, fa, theta = 30, phi = 20)
&gt; persp(x, y, fa, theta = 30, phi = 70)
&gt; persp(x, y, fa, theta = 30, phi = 40)
&gt; A = matrix(1:16, 4, 4)
&gt; A
     [,1] [,2] [,3] [,4]
[1,]    1    5    9   13
[2,]    2    6   10   14
[3,]    3    7   11   15
[4,]    4    8   12   16
&gt; A[2, 3]
[1] 10
&gt; A[c(1, 3), c(2, 4)]
     [,1] [,2]
[1,]    5   13
[2,]    7   15
&gt; A[1:3, 2:4]
     [,1] [,2] [,3]
[1,]    5    9   13
[2,]    6   10   14
[3,]    7   11   15
&gt; A[1:2,]
     [,1] [,2] [,3] [,4]
[1,]    1    5    9   13
[2,]    2    6   10   14
&gt; A[, 1:2]
     [,1] [,2]
[1,]    1    5
[2,]    2    6
[3,]    3    7
[4,]    4    8
&gt; A[1,]
[1]  1  5  9 13
&gt; A[-c(1 ,3),]
     [,1] [,2] [,3] [,4]
[1,]    2    6   10   14
[2,]    4    8   12   16
&gt; A[-c(1 ,3), -c(1, 3, 4)]
[1] 6 8
&gt; dim(A)
[1] 4 4
&gt; Auto = read.table(&quot;Auto.data&quot;)
&gt; fix(Auto)
&gt; Auto = read.table(&quot;Auto.data&quot;, header = T, na.strings = &quot;?&quot;)
&gt; fix(Auto)
&gt; Auto = read.csv(&quot;Auto.csv&quot;, header = T, na.strings = &quot;?&quot;)
&gt; fix(Auto)
&gt; dim(Auto)
[1] 397   9
&gt; Auto[1:4,]
  mpg cylinders displacement horsepower weight acceleration year origin
1  18         8          307        130   3504         12.0   70      1
2  15         8          350        165   3693         11.5   70      1
3  18         8          318        150   3436         11.0   70      1
4  16         8          304        150   3433         12.0   70      1
                       name
1 chevrolet chevelle malibu
2         buick skylark 320
3        plymouth satellite
4             amc rebel sst
&gt; Auto = na.omit(Auto)
&gt; dim(Auto)
[1] 392   9
&gt; names(Auto)
[1] &quot;mpg&quot;          &quot;cylinders&quot;    &quot;displacement&quot; &quot;horsepower&quot;   &quot;weight&quot;      
[6] &quot;acceleration&quot; &quot;year&quot;         &quot;origin&quot;       &quot;name&quot;        
&gt; plot(cylinders, mpg)
Error in plot(cylinders, mpg) : object &apos;cylinders&apos; not found
&gt; plot(Auto\$cylinders, Auto\$mpg)
&gt; attach(Auto)
&gt; plot(cylinders, mpg)
&gt; cylinders = as.factor(cylinders)
&gt; plot(cylinders, mpg)
&gt; plot(cylinders, mpg, col = &quot;red&quot;)
&gt; plot(cylinders, mpg, col = &quot;red&quot;, varwidth = T)
&gt; plot(cylinders, mpg, col = &quot;red&quot;, varwidth = T, horizontal = T)
&gt; plot(cylinders, mpg, col = &quot;red&quot;, varwidth = T, xlab = &quot;cylinders&quot;, ylab = &quot;MPG&quot;)
&gt; hist(mpg)
&gt; hist(mpg, col = 2)
&gt; hist(mpg, col = 2, breaks = 15)
&gt; pairs(Auto)
&gt; pairs(~ mpg + displacement + horsepower + weight + acceleration, Auto)
&gt; plot(horsepower, mpg)
&gt; identify(horsepower, mpg, name)
&gt; summary(Auto)
      mpg          cylinders      displacement     horsepower        weight    
 Min.   : 9.00   Min.   :3.000   Min.   : 68.0   Min.   : 46.0   Min.   :1613  
 1st Qu.:17.00   1st Qu.:4.000   1st Qu.:105.0   1st Qu.: 75.0   1st Qu.:2225  
 Median :22.75   Median :4.000   Median :151.0   Median : 93.5   Median :2804  
 Mean   :23.45   Mean   :5.472   Mean   :194.4   Mean   :104.5   Mean   :2978  
 3rd Qu.:29.00   3rd Qu.:8.000   3rd Qu.:275.8   3rd Qu.:126.0   3rd Qu.:3615  
 Max.   :46.60   Max.   :8.000   Max.   :455.0   Max.   :230.0   Max.   :5140  
                                                                               
  acceleration        year           origin                      name    
 Min.   : 8.00   Min.   :70.00   Min.   :1.000   amc matador       :  5  
 1st Qu.:13.78   1st Qu.:73.00   1st Qu.:1.000   ford pinto        :  5  
 Median :15.50   Median :76.00   Median :1.000   toyota corolla    :  5  
 Mean   :15.54   Mean   :75.98   Mean   :1.577   amc gremlin       :  4  
 3rd Qu.:17.02   3rd Qu.:79.00   3rd Qu.:2.000   amc hornet        :  4  
 Max.   :24.80   Max.   :82.00   Max.   :3.000   chevrolet chevette:  4  
                                                 (Other)           :365  
&gt; summary(mpg)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   9.00   17.00   22.75   23.45   29.00   46.60 
&gt; q()
Save workspace image? [y/n/c]: n
&lt;file://bailey.hn.y.st/home/yst&gt;
&gt;</pre>
</blockquote>
<h2>Figures</h2>
<p>
	I didn&apos;t screenshot the output of <code>identify(horsepower, mpg, name)</code> because there were just too many options of what it could look like, depending on which data points you click.
	I tried clicking all of them, and the words added to the image jumbled together and made a garbled mess not worth showing.
	For all other images and tables, a screenshot is provided below.
</p>
<img src="/img/CC_BY-SA_4.0/y.st./coursework/CS4407/figure_1-00.png" alt="Figure 1-00" class="framed-centred-image" width="677" height="699"/>
<img src="/img/CC_BY-SA_4.0/y.st./coursework/CS4407/figure_1-01.png" alt="Figure 1-01" class="framed-centred-image" width="677" height="699"/>
<img src="/img/CC_BY-SA_4.0/y.st./coursework/CS4407/figure_1-02.png" alt="Figure 1-02" class="framed-centred-image" width="808" height="751"/>
<img src="/img/CC_BY-SA_4.0/y.st./coursework/CS4407/figure_1-03.png" alt="Figure 1-03" class="framed-centred-image" width="677" height="699"/>
<img src="/img/CC_BY-SA_4.0/y.st./coursework/CS4407/figure_1-04.png" alt="Figure 1-04" class="framed-centred-image" width="677" height="699"/>
<img src="/img/CC_BY-SA_4.0/y.st./coursework/CS4407/figure_1-05.png" alt="Figure 1-05" class="framed-centred-image" width="677" height="699"/>
<img src="/img/CC_BY-SA_4.0/y.st./coursework/CS4407/figure_1-06.png" alt="Figure 1-06" class="framed-centred-image" width="677" height="699"/>
<img src="/img/CC_BY-SA_4.0/y.st./coursework/CS4407/figure_1-07.png" alt="Figure 1-07" class="framed-centred-image" width="677" height="699"/>
<img src="/img/CC_BY-SA_4.0/y.st./coursework/CS4407/figure_1-08.png" alt="Figure 1-08" class="framed-centred-image" width="677" height="699"/>
<img src="/img/CC_BY-SA_4.0/y.st./coursework/CS4407/figure_1-09.png" alt="Figure 1-09" class="framed-centred-image" width="677" height="699"/>
<img src="/img/CC_BY-SA_4.0/y.st./coursework/CS4407/figure_1-10.png" alt="Figure 1-10" class="framed-centred-image" width="677" height="699"/>
<img src="/img/CC_BY-SA_4.0/y.st./coursework/CS4407/figure_1-11.png" alt="Figure 1-11" class="framed-centred-image" width="677" height="699"/>
<img src="/img/CC_BY-SA_4.0/y.st./coursework/CS4407/figure_1-12.png" alt="Figure 1-12" class="framed-centred-image" width="611" height="655"/>
<img src="/img/CC_BY-SA_4.0/y.st./coursework/CS4407/figure_1-13.png" alt="Figure 1-13" class="framed-centred-image" width="611" height="655"/>
<img src="/img/CC_BY-SA_4.0/y.st./coursework/CS4407/figure_1-14.png" alt="Figure 1-14" class="framed-centred-image" width="611" height="655"/>
<img src="/img/CC_BY-SA_4.0/y.st./coursework/CS4407/figure_1-15.png" alt="Figure 1-15" class="framed-centred-image" width="677" height="699"/>
<img src="/img/CC_BY-SA_4.0/y.st./coursework/CS4407/figure_1-16.png" alt="Figure 1-16" class="framed-centred-image" width="677" height="699"/>
<img src="/img/CC_BY-SA_4.0/y.st./coursework/CS4407/figure_1-17.png" alt="Figure 1-17" class="framed-centred-image" width="677" height="699"/>
<img src="/img/CC_BY-SA_4.0/y.st./coursework/CS4407/figure_1-18.png" alt="Figure 1-18" class="framed-centred-image" width="677" height="699"/>
<img src="/img/CC_BY-SA_4.0/y.st./coursework/CS4407/figure_1-19.png" alt="Figure 1-19" class="framed-centred-image" width="677" height="699"/>
<img src="/img/CC_BY-SA_4.0/y.st./coursework/CS4407/figure_1-20.png" alt="Figure 1-20" class="framed-centred-image" width="677" height="699"/>
<img src="/img/CC_BY-SA_4.0/y.st./coursework/CS4407/figure_1-21.png" alt="Figure 1-21" class="framed-centred-image" width="677" height="699"/>
<img src="/img/CC_BY-SA_4.0/y.st./coursework/CS4407/figure_1-22.png" alt="Figure 1-22" class="framed-centred-image" width="677" height="699"/>
<img src="/img/CC_BY-SA_4.0/y.st./coursework/CS4407/figure_1-23.png" alt="Figure 1-23" class="framed-centred-image" width="677" height="699"/>
<img src="/img/CC_BY-SA_4.0/y.st./coursework/CS4407/figure_1-24.png" alt="Figure 1-24" class="framed-centred-image" width="677" height="699"/>
<img src="/img/CC_BY-SA_4.0/y.st./coursework/CS4407/figure_1-25.png" alt="Figure 1-25" class="framed-centred-image" width="677" height="699"/>
<img src="/img/CC_BY-SA_4.0/y.st./coursework/CS4407/figure_1-26.png" alt="Figure 1-26" class="framed-centred-image" width="677" height="699"/>
<img src="/img/CC_BY-SA_4.0/y.st./coursework/CS4407/figure_1-27.png" alt="Figure 1-27" class="framed-centred-image" width="677" height="699"/>
END
);
